﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Store.Model.Models;
using Store.Bll;
using Store.ViewModel;
using System.IO;
using Microsoft.AspNetCore.Cors;
using StackExchange.Redis;
using Store.Helper;
using Newtonsoft.Json;

namespace Store.Server.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]

    [EnableCors("any")]
    public class OrderController : Controller
    {
        private readonly HandlingOrderBll bll;
        private readonly IDatabase _redis;
        public OrderController(HandlingOrderBll _bll, RedisHelper client)
        {
            bll = _bll;
            _redis = client.GetDatabase();
        }

        //显示订单表
        [HttpGet]
        public IActionResult Index(int sort=0,int state=0, string number="", string user="",
            string time="", int page=1, int limit=20)
        {
            int total;
            var data = bll.GetOrders(sort,state, number, user, time, page, limit, out total);
            return Json(new { data = data, total = total});
        }

        //显示订单信息
        [HttpGet]
        public ObjectResult GetOrder(int id)
        {
            var data= bll.GetOrder(id);
            return Ok(data);
        }

        //显示收货人信息
        [HttpGet]
        public ObjectResult GetUserInfo(int id)
        {
            var data = bll.GetUserInfo(id);
            return Ok(data);
        }

        //显示商品信息
        [HttpGet]
        public ObjectResult GetCommodity(int id)
        {
            var data = bll.GetCommodity(id);
            return Ok(data);
        }

        //显示费用合计
        [HttpGet]
        public List<CostView> GetCostViews(int id)
        {
            return bll.GetCostViews(id);
        }

        //修改用户
        [HttpPut]
        public int UpdateUser(int userid, string username, string userphone, string addressname, string addresspostal)//User user, Addresse addresse
        {
            return bll.UpdateUser(userid,username,userphone,addressname,addresspostal);
        }
        //修改用户地址和邮政编码
        //[HttpPut]
        //public int UpdateUserAddress(Addresse addresse)
        //{
        //    return bll.UpdateUserAddress(addresse);
        //}

        //添加订单
        [HttpPost]
        public int AddOrder(Model.Models.Order orders)
        {
            return bll.AddOrder(orders);
        }



        //修改订单状态
        [HttpPut]
        public int UpdOrderState(int id, int state)
        {
            return bll.UpdOrderState(id, state);
        }

        //批量修改状态
        [HttpPut]
        public int UpdOrderStates(string ids)
        {
            string[] id = ids.Split(',');
            return bll.UpdOrderStates(id);
        }
    }
}
